﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCG.SolarVengeanceEngine;
using SCG.SolarVengeanceEngine.StarShips;

namespace SCG.SolarVengeance
{
    public partial class CommandPanel : BasePanel
    {
        //constructors
        public CommandPanel()
            : base()
        {
            InitializeComponent();
        }

        //update every impulse
        public override void ImpulseUpdate()
        {
            switch (OrderType)
            {
                case OrderType.Patrol:
                    btnCompleteOrder.Visible = GamePage.Waypoints.Count > 1;
                    if (GamePage.Waypoints.Count == 1)
                        lblDescription.Text = "Click the next Patrol Point on the map.";
                    else if (GamePage.Waypoints.Count > 1)
                        lblDescription.Text = "Click the next Patrol Point on the map, or Complete the Order now.";
                    break;
                case OrderType.Transport:
                    if (GamePage.TransportPoints.Count > 1)
                    {
                        SVMapObject lastPoint = GamePage.TransportPoints[GamePage.TransportPoints.Count - 1];
                        if (lastPoint is StarSystem)
                            btnCompleteOrder.Visible = true;
                        else
                        {
                            ResourceStarShip rss = lastPoint as ResourceStarShip;
                            btnCompleteOrder.Visible = rss.IsTransportDestination;
                        }
                    }
                    if (!btnCompleteOrder.Visible)
                        lblDescription.Text = "Click another StarSystem" + GetResourceStarShipString() + ".";
                    else
                        lblDescription.Text = "Click another StarSystem" + GetResourceStarShipString() + ", or Complete the Order now to make the last selected object the Transport destination.";
                    break;
                case OrderType.SetViewPoint:
                    lblDescription.Text = "Click the point on the map that will become the center of the Remote Viewer's View Range.";
                    break;
                default:
                    btnCompleteOrder.Visible = false;
                    break;
            }
            Refresh();
        }

        //update the error text
        public string Error
        {
            get
            {
                return lblError.Text;
            }
            set
            {
                lblError.Text = value;
            }
        }

        //what order is being executed?
        private OrderType _orderType;
        public OrderType OrderType
        {
            get
            {
                return _orderType;
            }
            set
            {
                _orderType = value;
                lblOrder.Text = _orderType.ToString();
                btnCompleteOrder.Visible = false;
                switch (_orderType)
                {
                    case OrderType.Patrol:
                        lblDescription.Text = "Click the first in a series of Patrol Points on the map.";
                        break;
                    case OrderType.Escort:
                        lblDescription.Text = "Click a StarShip to Escort.  Your StarShip will maintain its relative position to the target StarShip.";
                        break;
                    case OrderType.Defend:
                        lblDescription.Text = "Click an item or location on the map to Defend.  Your StarShip will move to intercept any enemies that approach what you are defending.";
                        break;
                    case OrderType.Transport:
                        lblDescription.Text = "Click a StarSystem" + GetResourceStarShipString() + " to collect Resources from.";
                        break;
                    case OrderType.Unload:
                        lblDescription.Text = "Click a StarSystem" + GetResourceStarShipString() + " to Unload your Resources onto.";
                        break;
                }
                Error = "";
            }
        }

        //return a string that contains the available resource-carrying starship types
        public string GetResourceStarShipString()
        {
            string s = "";
            List<StarShipType> st = new List<StarShipType>();
            foreach(StarShip ship in Game.CurrentPlayer.StarShips)
                if (!st.Contains(ship.ShipType))
                    if (ship is ResourceStarShip)
                    {
                        st.Add(ship.ShipType);
                        if (s != "")
                            s += "/";
                        s += ship.DisplayName;
                    }
            if (s != "")
                s = " or " + s;
            return s;
        }

        //cancel special order, return to basic starship view
        private void btnCancelOrder_Click(object sender, EventArgs e)
        {
            GamePage.Waypoints.Clear();
            GamePage.TransportPoints.Clear();
            GamePage.SidePanelMode = SidePanels.StarShip;
        }

        //complete the order
        private void btnCompleteOrder_Click(object sender, EventArgs e)
        {
            foreach(StarShip ship in GamePage.ShipsSelected)
                if (ship.CanExecuteOrder(OrderType))
                {
                    switch (OrderType)
                    {
                        case OrderType.Patrol:
                            GamePage.SubmitPatrolOrder(ship);
                            break;
                        case OrderType.Transport:
                            GamePage.SubmitTransportOrder(ship);
                            break;
                        default:
                            throw new ArgumentException("Unexpected OrderType: " + OrderType);
                    }
                }
            GamePage.Waypoints.Clear();
            GamePage.TransportPoints.Clear();
            GamePage.SidePanelMode = SidePanels.Blank;
        }
    }
}
